import {
  OrderModel
} from '../../models/hdd/orderData.js'
let orderModel = new OrderModel()
var sliderWidth = 108;
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  pageLifetimes: {
    show() {
      wx.setNavigationBarTitle({
        title: '订单'
      })
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#FF7136',
        animation: {
          duration: 400,
          timingFunc: 'easeIn'
        }
      })
      var that = this;
      wx.getSystemInfo({
        success: function(res) {
          that.setData({
            sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
            sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
          });
        }
      });
      this.requestData()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabs: ["已发布", "已抢单", "待付款", "已完成"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    listData1: [],
    listData2: [],
    listData3: [],
    listData4: [],
    ifSearch: false,
    searchKey: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    requestData() {
      wx.showLoading({
        mask: true
      })
      var schemaIdTmp
      if (this.data.activeIndex == 0) {
        schemaIdTmp = 'da1b0b64-b08b-463b-8210-facceec2624a'
      } else if (this.data.activeIndex == 1) {
        schemaIdTmp = '943730b7-6def-4a96-ae15-274a6f4fa6f1'
      } else if (this.data.activeIndex == 2) {
        schemaIdTmp = 'c0a5ad42-3c64-40f8-adb3-eeddf74e8928'
      } else {
        schemaIdTmp = '1c4481f1-7a59-4cdb-8331-286215ce42f9'
      }
      let data = orderModel.getOrderList({
        listId: '285f7202-2b0a-42b1-ab1b-9302f53917db',
        unitId: 'c91854d1-b301-49fb-aba1-7cac8e161e8e',
        pageCode: 'ATC.Order.List',
        schemaId: schemaIdTmp,
        keyword: this.data.searchKey
      })
      data.then(res => {
        console.log(res)
        if (this.data.activeIndex == 0) {
          this.setData({
            listData1: res.data.data.data.records
          })
        } else if (this.data.activeIndex == 1) {
          this.setData({
            listData2: res.data.data.data.records
          })
        } else if (this.data.activeIndex == 2) {
          this.setData({
            listData3: res.data.data.data.records
          })
        } else {
          this.setData({
            listData4: res.data.data.data.records
          })
        }
        wx.hideLoading()
      })
    },

    tabClick: function(e) {
      if (this.data.activeIndex == e.currentTarget.id) {
        return
      }
      this.setData({
        sliderOffset: e.currentTarget.offsetLeft,
        activeIndex: e.currentTarget.id
      });
      console.log(this.data.activeIndex)
      this.requestData()
    },

    clickOrder(event) {
      wx.navigateTo({
        url: `/pages/createOrder/createOrder?FId=${event.detail.orderData.FId}&companyId=${event.detail.orderData.FCompany}`,
      })
    },

    clickFixer(event) {
      wx.navigateTo({
        url: `/pages/personInfo/personInfo?FId=${event.detail.orderData.FId}`,
      })
    },

    searchOrder() {
      console.log(this.data.activeIndex)
      if(this.data.searchKey.length == 0) {
        return
      }
      this.requestData(this.data.activeIndex)
    },

    searchInput(e) {
      this.setData({
        searchKey: e.detail.value
      })
    },

    startSearch() {
      this.setData({
        ifSearch: true
      })
    },

    endSearch() {
      if (this.data.searchKey.length == 0) {
        this.setData({
          ifSearch: false
        })
      }
    },

    createOrder() {
      wx.navigateTo({
        url: '/pages/createOrder/createOrder',
      })
    }
  }
})